// packageA/pages/evaluate-goods/index.js
const baseUrl = require("../../../api/config").dev; //这里用的是ES6的写法
const WXAPI = require("../../../api/api");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    iAnonymous:0,
    checked: false,
    objStr: {
      goodsList: [],
    },
    imgBaseUrl: `${baseUrl["imgBaseUrl"]}`,
    title: "评价",
    dEvaluateScore: 1,
    dDeliveryPackagingScore: 1,
    dDeliverySpeedScore: 1,
    dDeliveryServiceScore: 1,
    sEvaluateContent: "",
    form: {},
  },
  onChange(e) {
    this.setData({
      checked: e.detail,
    });
    this.setData({
      iAnonymous: this.data.checked ? 1 : 0,
    });
  },
  onChangeStar1(e) {
    const { index } = e.currentTarget.dataset;
    let obj = this.data.objStr;
    obj.goodsList[index].dEvaluateScore = e.detail;
    this.setData({
      objStr: obj,
    });
  },
  onChangeStar2(e) {
    this.setData({
      dDeliveryPackagingScore: e.detail,
    });
  },
  onChangeStar3(e) {
    this.setData({
      dDeliverySpeedScore: e.detail,
    });
  },
  onChangeStar4(e) {
    this.setData({
      dDeliveryServiceScore: e.detail,
    });
  },
  inputValue(e) {
    const { name, index } = e.currentTarget.dataset;
    const value = e.detail; //值
    const obj = this.data.objStr;
    obj.goodsList[index].sEvaluateContent = value;
    this.setData({
      objStr: obj,
    });
  },
  submit() {
    let _this = this;
    wx.showModal({
      title: "提示",
      content: "确认提交评价？",
      complete: async (res) => {
        if (res.confirm) {
          let id = _this.data.objStr.id;
          let data = {
            iAnonymous: _this.data.iAnonymous,
            sOrgId: _this.data.objStr.sOrgId,
            EvaluateGoods: [],
            EvaluateLogistics: {
              sOrderId: _this.data.objStr.id,
              dDeliveryPackagingScore: this.data.dDeliveryPackagingScore,
              dDeliverySpeedScore: this.data.dDeliverySpeedScore,
              dDeliveryServiceScore: this.data.dDeliveryServiceScore,
              // Attachments: [],
            },
          };
          // 商品信息
          for (let i = 0; i < this.data.objStr.goodsList.length; i++) {
            data.EvaluateGoods.push({
              sOrderId: _this.data.objStr.id,
              sGoodsId: this.data.objStr.goodsList[i].sGoodsId,
              dEvaluateScore: this.data.objStr.goodsList[i].dEvaluateScore,
              sEvaluateContent: this.data.objStr.goodsList[i].sEvaluateContent,
              Attachments: this.data.objStr.goodsList[i].Attachments,
            });
          }
          let res = await WXAPI.createGoodsEvaluate(id, data);
          if (res.data.code == 0) {
            wx.showToast({
              title: res.data.message,
            })
            // setTimeout(() => {
            //   wx.navigateTo({
            //     url: `/packageA/pages/all-order/index?orderType=1&navigationBarTitle=全部订单&status=0`,
            //   })
            // }, 2000);
            setTimeout(() => {
              var page = getCurrentPages();
              var prevPage = page[page.length - 2];
              // 存储上一页数据
              // prevPage.setData({
              //   orderType: 1,
              //   navigationBarTitle: "全部订单",
              //   status:0
              // });
              // 返回刷新
              prevPage.onLoad();
              wx.navigateBack({
                detail: 1,
              });
            }, 2000);
          }
        }
      },
    });
  },
  deleteFile(e) {
    let _this = this;
    const {index} = e.currentTarget.dataset
    let obj = this.data.objStr
    wx.showModal({
      title: "提示",
      content: "是否确认删除？",
      complete: (res) => {
        if (res.confirm) {
          const { id } = e.detail.file;
          let i = obj[index].fileList.findIndex((item) => item.id == id);
          obj[index].fileList.splice(i, 1);
          let j = obj.goodsList[index].Attachments.findIndex((item) => item == id);
          obj.goodsList[index].Attachments.splice(j, 1);
          _this.setData({
            objStr:obj
          });
        }
      },
    });
  },
  afterRead(event) {
    console.log("event.detail", event);
    const { file } = event.detail;
    const { index } = event.currentTarget.dataset;
    let obj = this.data.objStr;
    let _this = this;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    wx.uploadFile({
      url: baseUrl["baseUrl"] + "/api/Files", //服务器端接收图片的路径
      filePath: file.url,
      name: "file",
      header: { Authorization: wx.getStorageSync("token") },
      formData: {
        name: `test${new Date().getTime()}.png`,
        sBusinessType: 'ProductOrderEvaluateGoods',
      },
      success(res) {
        let data = JSON.parse(res.data).data;
        // 上传完成需要更新 fileList
        if(!obj.goodsList[index].fileList) {
          obj.goodsList[index].fileList = []
        }
        obj.goodsList[index].fileList.push({
          ...file,
          url: _this.data.imgBaseUrl + data.sFileUrl,
          id: data.sId,
        });
        if(!obj.goodsList[index].Attachments) {
          obj.goodsList[index].Attachments = []
        }
        
        obj.goodsList[index].Attachments.push(data.sId);
        _this.setData({
          objStr: obj,
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.type) {
      wx.setNavigationBarTitle({
        title: options.type == "1" ? "商品评价" : "服务评价",
      });
    }
    if (options.objStr) {
      this.setData({
        objStr: JSON.parse(options.objStr),
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
